import bean.Corporation;
import bean.Person;
import config.SpringConfig;
import context.CustomApplicationEvent;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.*;

public class Demo {
    private AnnotationConfigApplicationContext context;
    
    @Before
    public void setUp() {
        // 加载应用程序上下文
        context = new AnnotationConfigApplicationContext();
        context.register(SpringConfig.class);
        context.refresh();
    }
    
    @Test
    public void testContext() {
        Date now = (Date) context.getBean("now");
        System.out.println("当前系统默认时区时间：" + now);
        Person firstPerson = (Person) context.getBean("p1");
        System.out.println(firstPerson);
        Person secondPerson = (Person) context.getBean("p2");
        System.out.println(secondPerson);
        Person thirdPerson = (Person) context.getBean("p3");
        System.out.println(thirdPerson);
        Corporation corporation = (Corporation) context.getBean("corporation");
        String[] departments = corporation.getDepartments();
        System.out.println("该公司一共有" + departments.length + "个部门");
        System.out.println(departments[0]);
        System.out.println(departments[1]);
        System.out.println(departments[2]);
        System.out.println(departments[3]);
        List<String> positions = corporation.getPositions();
        System.out.println("每个部门下分" + positions.size() + "个职位");
        System.out.println(positions.get(0));
        System.out.println(positions.get(1));
        System.out.println(positions.get(2));
        Map<Integer, Person> employees = corporation.getEmployees();
        System.out.println("公司一共有" + employees.size() + "名员工");
        Set<Map.Entry<Integer, Person>> entrySet = employees.entrySet();
        for (Map.Entry<Integer, Person> entry : entrySet) {
            System.out.println(entry.getKey() + "：" + entry.getValue());
        }
        System.out.println(context.getMessage("msg", new Object[] { "你", "spring依赖注入" }, Locale.CHINA));
        System.out.println(context.getMessage("info", new String[] { "当前用户" }, Locale.CHINA));
        // 发布事件
        context.publishEvent(new CustomApplicationEvent(this));
        // AbstractApplicationContext类实现了Closeable接口，此类实例应当在使用完毕后调用close方法关闭
        context.close();
    }
}
